'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 铁匠铺排程
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var mongoose = require('mongoose');

///////////////////////////////////////////////////////////
/**
 * build schedule schema.
 * @type {mongoose}
 */
var smithyScheduleSchema = new mongoose.Schema({
    // 工匠槽
    smithy_id: {
        type: Number,
        required: true
    },
    // 工人id
    worker_id: {
        type: Number,
        required: true
    },

    // 结束时间
    end_time: {
        type: Date,
        required: true
    },

    // 生产时间
    total_time: {
        type: Number,
        required: true
    }
});

/**
 * 获取铁匠铺id
 * @return {[type]} [description]
 */
smithyScheduleSchema.methods.getWorkerId = function () {
    return this.worker_id;
};

/**
 * 获取结束时间
 * @return {[type]} [description]
 */
smithyScheduleSchema.methods.getEndTime = function () {
    return this.end_time;
};

/**
 * 获取剩余时间
 * @return {[type]} [description]
 */
smithyScheduleSchema.methods.getRemainTime = function () {
    var remain_time = parseInt(moment.duration(this.end_time - moment(), 'ms').asSeconds());
    if (remain_time <= 0) {
        return 0;
    }
    return remain_time;
};

if (!smithyScheduleSchema.options.toJSON) {
    smithyScheduleSchema.options.toJSON = {};
}
/* jshint unused:false */
smithyScheduleSchema.options.toJSON.transform = function (doc, ret) {
    ret.end_time = moment(ret.end_time).unix();
};

module.exports = smithyScheduleSchema;
